package com.whjz.dcfs.file.struts.action;

import com.whjz.dcfs.util.BaseAction;
import com.whjz.dcfs.util.UserUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;



import java.util.Map;

import com.whjz.dcfs.util.excel.ExcelExporterUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.whjz.dcfs.file.dao.FileDao;
import com.whjz.dcfs.file.model.FileInfo;
import com.whjz.dcfs.file.model.PageBean;
import com.whjz.dcfs.file.model.UserConfig;

public class FileAction extends BaseAction {

	private static final long serialVersionUID = -2198125622912789099L;
	
    private File   Filedata;//附件
    private String FiledataFileName;//附件名字
    private String FiledataContentType;//附件类型
    private int fileSize;//附件大小
    private List<FileInfo> fileList;//附件集合
    private String path;//物理路径
    private InputStream downLoadFile;//导出文件的输入流
    private String fileId;//文件ID
    private String userId;//用户ID
	private int start;
	private int limit;
	private String fileParams;//查询参数
	private String fileLable;//文件类型
	private Date startDate;//开始时间
	private Date endDate;//结束时间
	private PageBean<FileInfo> pageFile;
	@Autowired 
    public FileDao fileDao;
	public String execute(){
		return SUCCESS;
	}
	/***
	 * 上传附件
	 * @Exception
	 * */
	public String uploadFile(){
		PrintWriter out=null;
		try {
			Thread.sleep(1);
			FileInfo file=new FileInfo();
			String fileId=String.valueOf((new Date()).getTime());
			file.setFileId(fileId);
			file.setUserId(UserUtil.findUserFromSession(getSession().get("user"),"getUserId"));
			file.setFileName(FiledataFileName);
			file.setFileSize(fileSize);
			file.setFileType(FiledataContentType);
			file.setFileLable(fileLable);
			file.setFileCreateDate(new Date());
			fileDao.saveFile(file);
//			//保存文件
			if (fileSize>0) {
				String savePath = path + File.separator +"";
				File saveDir = new File(savePath);
				//没有企业文件夹则新建
				if (!saveDir.exists()) {
					saveDir.mkdirs();
				}
				if("视频".equals(fileLable)){
					fileId=fileId+"."+FiledataFileName.split("\\.")[1];
				}
				File saveFile = new File(savePath + File.separator +fileId);
				FileUtils.copyFile(Filedata, saveFile);

			}
			out=getHttpServletResponse().getWriter();
			out.write("{success:true}");
		} catch (Exception e) {
			out.write("{error:true}");
			e.printStackTrace();
		}
		return  null;
	}
	/***
	 * 查询附件
	 * */
	@SuppressWarnings("unchecked")
	public String selectFileInfo(){
			try {
				@SuppressWarnings("rawtypes")
				Map map = new HashMap();
				map.put("start", start);
				map.put("pageLast", start+limit);
				if((!"".equals(fileParams))&&fileParams!=null){
					map.put("fileName", "%"+fileParams+"%");
				}
				map.put("fileLable",fileLable);
				map.put("startDate", startDate);
				map.put("endDate", endDate);
				pageFile= fileDao.selectFileInfo(map);
			} catch (Exception e) {
				e.printStackTrace();
			}
		return "json";
	}
	/***
	 * 下载附件
	 * */
	public String downLoadFile(){
		try {
			FiledataFileName=URLEncoder.encode(FiledataFileName,"UTF-8");
//				FiledataFileName = new String(FiledataFileName.getBytes(),"utf-8");
			downLoadFile=new FileInputStream(new File(path+File.separator+fileId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "downLoad";
	}

	public String downloadExcelTemplate() {
		try {
			FiledataFileName = "基站增加模板.xls";
			FiledataFileName=URLEncoder.encode(FiledataFileName,"UTF-8");
			String excelPath = "template" + File.separator + "add_jz_template.xls";
			FiledataContentType = "application/octet-stream";
			downLoadFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "downLoad";
	}

	/***
	 * 删除附件
	 * */
	public String removeFile(){
		PrintWriter out=null;
		try {
			out=getHttpServletResponse().getWriter();
			fileDao.removeFile(fileId);
			File removeFile=new File(path+File.separator+fileId);
			if(removeFile.exists()){
				removeFile.delete();
			}
			out.write("true");
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			out.write("");
			return null;
		}finally{
			out.close();
		}
	}
	/***
	 * 根据ID查询用户姓名
	 * params:userId
	 * 参数描述：用户ID
	 * */
	public String findNameByUserId(){
		PrintWriter out = null;
		try {
			out=getHttpServletResponse().getWriter();
			getHttpServletResponse().setCharacterEncoding("utf-8");
			getHttpServletResponse().setContentType("text/html:utf-8");
			String  userName=fileDao.findNameByUserId(userId);
			out.write(URLEncoder.encode(userName,"utf-8"));
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/***
	 * 查询任务附件信息
	 * */
	public String getFileInfoByCond(){
		try {
			Map map = new HashMap();
			map.put("fileLable", "视频");
			fileList=fileDao.selectFileInfoByCond(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "video";
	}
	public String gotoMain(){
		return "main";
	}
	public File getFiledata() {
		return Filedata;
	}
	public void setFiledata(File filedata) {
		Filedata = filedata;
	}
	public String getFiledataFileName() {
		return FiledataFileName;
	}
	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}
	public String getFiledataContentType() {
		return FiledataContentType;
	}
	public void setFiledataContentType(String filedataContentType) {
		FiledataContentType = filedataContentType;
	}
	public int getFileSize() {
		return fileSize;
	}
	public void setFileSize(int fileSize) {
		this.fileSize = fileSize;
	}
	public List<FileInfo> getFileList() {
		return fileList;
	}
	public void setFileList(List<FileInfo> fileList) {
		this.fileList = fileList;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getFileId() {
		return fileId;
	}
	public void setFileId(String fileId) {
		this.fileId = fileId;
	}
	public InputStream getDownLoadFile() {
		return downLoadFile;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public PageBean<FileInfo> getPageFile() {
		return pageFile;
	}
	public void setPageFile(PageBean<FileInfo> pageFile) {
		this.pageFile = pageFile;
	}
	public String getFileParams() {
		return fileParams;
	}
	public void setFileParams(String fileParams) {
		this.fileParams = fileParams;
	}
	public String getFileLable() {
		return fileLable;
	}
	public void setFileLable(String fileLable) {
		this.fileLable = fileLable;
	}
	public Date getStartDate() {
		return startDate;
	}
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
}
